﻿using BBCore.Core.Models.Admins;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BBCore.Application.Admins
{
    public class AnnouncementsService : IAnnouncementsService
    {
        //private readonly IFreeSql FreeSql;
        //public AnnouncementsService(IFreeSql freeSql) => FreeSql = freeSql;

        public bool IsAnnouncementRead(int announcementId, int userid)
        {
            return UserAnnouncementRead.Where(a => a.UserId == userid && a.AnnouncementId == announcementId).Any();
        }

        public async Task<List<Announcement>?> LoadAnnouncements_TopAsync()
        {
            //var lst = await FreeSql.Select<Announcement>().OrderByDescending(a => a.PublishTime).Take(5).ToListAsync();
            var lst = await Announcement.Select.OrderByDescending(a => a.PublishTime).Take(3).ToListAsync();
            return lst;

        }

        public async Task MarkAsReadAsync(int announcementId, int userid)
        {
            //await FreeSql.Insert(new UserAnnouncementRead
            //{
            //    AnnouncementId = announcementId,
            //    UserId = userid
            //}).ExecuteAffrowsAsync();
            var item = new UserAnnouncementRead
            {
                AnnouncementId = announcementId,
                UserId = userid
            };
            await item.InsertAsync();
        }
    }
}
